const { videoAds } = require("../ads/video");
const { show, msg } = require("../game/alert");
const { player, shopData, time } = require("../shop/gameData");
const { prefabArray, PrefabCache,  getPrefabPool, putPrefabPool } = require("../util/gameResources");

cc.Class({
    extends: cc.Component,
    
    properties: {
        shopList:cc.Node,//挂载对象
        tmpPrefab:cc.Prefab,
        myRank: cc.Node,
    },
    
    onLoad() {
        this.app = cc.cloud && cc.cloud.initialize();
        this.auth = this.app.auth();
        cc.log(player);
        cc.log(shopData);
        cc.log(time);
    },
    calMyData() {
        let money = 0;
        let count = 0;
        shopData.shopList.forEach(val => {
            money += val.money;
            if(val.status != 0) {
                count ++;
            }
        })
        money += player.money;
        this.rankInfo = {
            money,
            name: cc.sys.localStorage.getItem('shopName'),
            time: time.day,
            count
        }
        this.setMyRank();
    },
    upRank() {
        if(player.money < 100000) {
            msg('10w都没有还富豪？');
            return;
        }
        if(player.money < 10000){
            show('广告商赞助','是否让广告商赞助,需要1w元参与富豪榜',r => {
                videoAds.showAd(res => {
                    if(res) {
                        cc.log(res);
                        this.app.callFunction({
                            // 云函数名称，要与上传的函数名一致
                            name: "rank",
                            // 传给云函数的参数
                            data: {
                                action: "upRank",
                                params: this.rankInfo,
                            }
                        }).then(res => {
                            console.log('function', res);
                            this.shopList.children.forEach(val => {
                                // putPrefabPool("rank", val);
                                val.destroy();
                                this.shopList.height -= 105;
                            })
                            this.updateShop(res.result.data);
                        }).catch(console.error);
                    }else {
                        return ;
                    }
                })
            })
        }else { 
            show('未来富豪榜', '参与福步撕富豪榜需要支付1w元,确认吗？', (res) => {
                if(res){
                    player.money -= 10000;
                    this.app.callFunction({
                        // 云函数名称，要与上传的函数名一致
                        name: "rank",
                        // 传给云函数的参数
                        data: {
                            action: "upRank",
                            params: this.rankInfo,
                        }
                    }).then(res => {
                        console.log('function', res);
                        this.shopList.children.forEach(val => {
                            // putPrefabPool("rank", val);
                            val.destroy();
                            this.shopList.height -= 105;
                        })
                        this.updateShop(res.result.data);
                    }).catch(console.error);
                }else {
                    return ;
                }
            })
        }
        
        
       
    },
    updateShop(data){
            data = data.slice(0, 10);
            let myName = cc.sys.localStorage.getItem('shopName');
            data.forEach((val, index)=>{
                if(val.name == myName) {
                    this.myData = val;
                    this.myData.rank = index + 1;
                }
                let item  = getPrefabPool('rank');
                this.shopList.addChild(item);
                item.getChildByName("index").getComponent(cc.Label).string = `${index + 1}`;
                item.getChildByName("money").getComponent(cc.Label).string = `${formatMoney(val.money)}元`;
                item.getChildByName("name").getComponent(cc.Label).string = `${val.name}`;
                item.getChildByName("time").getComponent(cc.Label).string = `${val.time}天`;
                item.getChildByName("count").getComponent(cc.Label).string = `${val.count}家`;
                 //
                this.shopList.height += 105;
             })
             this.setMyRank();
    },
    setMyRank() {
        let item = this.myRank;
        let val = {};
        if(this.myData && this.myData.count) {
            val = this.myData;
        }else {
            val = this.rankInfo;
            val.rank = '未上榜';
        }
                item.getChildByName("index").getComponent(cc.Label).string = `${val.rank}`;
                item.getChildByName("money").getComponent(cc.Label).string = `${formatMoney(val.money)}元`;
                item.getChildByName("name").getComponent(cc.Label).string = `${val.name}`;
                item.getChildByName("time").getComponent(cc.Label).string = `${val.time}天`;
                item.getChildByName("count").getComponent(cc.Label).string = `${val.count}家`;
    },
    onEnable () {
        this.calMyData();
        this.callCloudFunction();
    },
    onDisable() {
        this.shopList.children.forEach(val => {
            // putPrefabPool("rank", val);
            val.destroy();
            this.shopList.height -= 105;
        })
    },
    callCloudFunction(){
        
        this.auth.anonymousAuthProvider().signIn().then(res => {
            // 需要先做授权才能正常调用。使用匿名登陆方式访问云开发资源
            // 请到腾讯云后台 -> 云开发 -> 选择当前环境 -> 环境设置/登录授权中，确认已经启用匿名登录
            // 匿名登录有一定限制，后期请更换为自定义登录等其他方式。
            console.log('TCB inited');
            this.app.callFunction({
                // 云函数名称，要与上传的函数名一致
                name: "rank",
                // 传给云函数的参数
                data: {
                    action: "money"
                }
            }).then(res => {
                console.log('function', res);
                this.updateShop(res.result.data);
            }).catch(console.error);
        });
      },
    // update (dt) {},
});
